package com.infore.water.service;

import com.infore.water.dao.RealDataDao;
import com.infore.water.entity.CloneFactor;
import com.infore.water.entity.Data;
import com.infore.water.entity.Factor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RealDataService {

    @Autowired
    private RealDataDao realDataDao;

    public Map<String, Object> getRealDataByStationId(String stationId) {
        List<Map<String,Object>> list = realDataDao.queryRealDataByStationId(stationId);
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> mapList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            map.put("stationId",list.get(i).get("stationId"));
            map.put("stationName",list.get(i).get("stationName"));
            map.put("receiveTime",list.get(i).get("receiveTime"));
            map.put("sendTime",list.get(i).get("sendTime"));
            Map<String,Object> projectMap = new HashMap<>();
            projectMap.put("projectId",list.get(i).get("projectId"));
            projectMap.put("projectName",list.get(i).get("projectName"));
            projectMap.put("projectCname",list.get(i).get("projectCname"));
            projectMap.put("value",list.get(i).get("value"));
//            projectMap.put("standard",list.get(i).get("standard"));
            projectMap.put("unit",list.get(i).get("unit"));
            mapList.add(projectMap);
        }
        map.put("projectList",mapList);
        return map;
    }

    public List<Data> queryHistoryData(String stationId, String startTime, String endTime,String dataType,String pageNum) throws ParseException,CloneNotSupportedException {
        List<Data> list = new ArrayList<>();
        List<Map<String, Object>> dataList;
        SimpleDateFormat sdf;
        SimpleDateFormat asdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(dataType.equals("0")){
            sdf = new SimpleDateFormat("yyyyMMddHH");
            System.out.println(sdf.format(asdf.parse(startTime)));
            dataList = realDataDao.queryHistoryHourData(stationId,sdf.format(asdf.parse(startTime)),sdf.format(asdf.parse(endTime)));
        }else if(dataType.equals("1")){
            sdf = new SimpleDateFormat("yyyyMMdd");
            dataList =  realDataDao.queryHistoryDayData(stationId,sdf.format(asdf.parse(startTime)),sdf.format(asdf.parse(endTime)));
        }else {
            sdf = new SimpleDateFormat("yyyyMM");
            dataList =  realDataDao.queryHistoryMonthData(stationId,sdf.format(asdf.parse(startTime)),sdf.format(asdf.parse(endTime)));
        }
        List<Factor> factorList = realDataDao.queryFactorByStationId(stationId);
        for (int i=0;i<dataList.size();i++){
            boolean flag = true;
            for(int j=0;j<list.size();j++){
                if(dataList.get(i).get("time").toString().equals(list.get(j).getStime())){
                    flag = false;
                    List<Factor> valueList =  list.get(j).getProjectList();
//                    Factor factor = new Factor();
                    for(int z=0;z<valueList.size();z++){
                        if(Integer.parseInt(dataList.get(i).get("projectId").toString())==valueList.get(z).getProjectId()){
                            valueList.get(z).setValue(Double.parseDouble(dataList.get(i).get("value").toString()));
                        }
                    }
                    list.get(j).setProjectList(valueList);
//                    factor.setProjectId(Integer.parseInt(dataList.get(i).get("projectId").toString()));
//                    factor.setProjectName(dataList.get(i).get("projectName").toString());
//                    factor.setValue(Double.parseDouble(dataList.get(i).get("value").toString()));
//                    factor.setUnit(dataList.get(i).get("unit").toString());
//                    factor.setProjectCname(dataList.get(i).get("cname").toString());
//                    valueList.add(factor);
                }
            }
            if(flag){
                Data data = new Data();
                data.setStationId(Integer.parseInt(dataList.get(i).get("stationId").toString()));
                data.setStationName(dataList.get(i).get("stationName").toString());
                data.setStime(dataList.get(i).get("time").toString());
                data.setTime(asdf.format(sdf.parse(dataList.get(i).get("time").toString())));
                List<Factor> factors = CloneFactor.getclone(factorList);
                for(int z=0;z<factors.size();z++){
                    if(Integer.parseInt(dataList.get(i).get("projectId").toString())==factors.get(z).getProjectId()){
                        factors.get(z).setValue(Double.parseDouble(dataList.get(i).get("value").toString()));
                    }
                }
//                List<Factor> valueList = new ArrayList<>();
//                Factor factor = new Factor();
//                factor.setProjectId(Integer.parseInt(dataList.get(i).get("projectId").toString()));
//                factor.setProjectName(dataList.get(i).get("projectName").toString());
//                factor.setValue(Double.parseDouble(dataList.get(i).get("value").toString()));
//                factor.setUnit(dataList.get(i).get("unit").toString());
//                factor.setProjectCname(dataList.get(i).get("cname").toString());
//                valueList.add(factor);
                data.setProjectList(factors);
                list.add(data);
            }
        }
        if (pageNum!=null) {
            List<Data> dList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (i >=Integer.parseInt(pageNum) * 20 && i < (Integer.parseInt(pageNum) + 1) * 20) {
                    dList.add(list.get(i));
                }
            }
            return dList;
        }else {
            return list;
        }
    }
}
